﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook.AdminIstrator
{
    public partial class Products : System.Web.UI.Page
    {
        static string Id = "";
        static bool Insert = false;
        protected string Username = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    Username = Data.Mysession.Session.Username;
                    BindGird();
                    Insert = false;
                }
                else
                    Response.Redirect("/AdminIstrator/Login.aspx");
            }
        }
        //Bin data
        private void BindGird()
        {
            grdProducts.DataSource = Business.ProductsService.Products_GetByAll();
            grdProducts.DataBind();
            for (int i = 0; i < grdProducts.Items.Count; i++)
            {
                if (grdProducts.Items[i].Cells[7].Text != "" || grdProducts.Items[i].Cells[8].Text != "" || grdProducts.Items[i].Cells[11].Text != "")
                {
                    grdProducts.Items[i].Cells[7].Text = Business.PublisherService.Publisher_GetById(grdProducts.Items[i].Cells[7].Text)[0].PName;
                    grdProducts.Items[i].Cells[8].Text = Business.CategoryService.Category_GetById(grdProducts.Items[i].Cells[8].Text)[0].cName;
                    if (grdProducts.Items[i].Cells[11].Text == "1")
                    {
                        grdProducts.Items[i].Cells[11].Text = "Hiện";
                        ((LinkButton)grdProducts.Items[i].FindControl("lbtActive")).Text = "Ẩn";
                    }
                    if (grdProducts.Items[i].Cells[11].Text == "0")
                    {
                        grdProducts.Items[i].Cells[11].Text = "Ẩn";
                        ((LinkButton)grdProducts.Items[i].FindControl("lbtActive")).Text = "Hiện";
                    }
                }
            }
            if (grdProducts.PageCount <= 1)
            {
                grdProducts.PagerStyle.Visible = false;
            }    
        }
        //Load DropDow
        private void LoadDropDown()
        {
           
            ddlCate.Items.Clear();
            ddlPublish.Items.Clear();
            ddlCate.Items.Add(new ListItem("====Chọn danh mục====","0"));
            ddlPublish.Items.Add(new ListItem("====Chọn nhà xuất bản=====", "0"));
            List<Data.CategoryInfo> listCat = Business.CategoryService.Category_GetByAll();
            List<Data.PublisherInfo> listP = Business.PublisherService.Publisher_GetByAll();
            for (int i = 0; i < listCat.Count; i++)
            {
                ddlCate.Items.Add(new ListItem(listCat[i].cName, listCat[i].cID));
            }
            for (int i = 0; i < listP.Count; i++)
            {
                ddlPublish.Items.Add(new ListItem(listP[i].PName, listP[i].PId));
            }
            ddlPublish.DataBind();
            ddlCate.DataBind();
        }
        //Page sing
        protected void grdProducts_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdProducts.CurrentPageIndex = e.NewPageIndex;
            BindGird();
           
        }
        //Databound
        protected void grdProducts_ItemDatabound(object sender, DataGridItemEventArgs e)
        {
        //    ListItemType itemType = e.Item.ItemType;
        //    if (itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem)
        //    {
        //        object checkbox = e.Item.FindControl("chkSelect");
        //        if (checkbox != null)
        //        {
        //            ((CheckBox)checkbox).A
        //        }
        //    }
        }
        protected void grdProducts_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Edit":
                    Id = strCA;
                    Insert = false;
                    List<Data.ProductsInfo> listU = Business.ProductsService.Products_GetById(Id);
                    LoadDropDown();
                    ddlCate.Text = listU[0].CatId;
                    ddlPublish.Text = listU[0].PUId;
                    txtPCode.Text = listU[0].PCode;
                    txtPName.Text = listU[0].PName;
                    txtPPice.Text = listU[0].PPrice;
                    txtSaleOff.Text = listU[0].PSaleOff;
                    txtAuthor.Text = listU[0].PAuthor;
                    txtStatus.Text = listU[0].PStatus;
                    //txtPImg.Text = listU[0].PImg;
                    lblFile.Text = listU[0].PImg;
                    PImgEdit.ImageUrl = listU[0].PImg;
                    chkPActive.Checked = listU[0].PActive == "1" ? true : false;
                    pnUpdate.Visible = true;
                    pnView.Visible = false;
                    break;
                case "Delete":
                    Business.ProductsService.Products_Delete(Id);
                    BindGird();
                    break;

                case "Active":
                    string strA = "";
                    string str = e.Item.Cells[11].Text;
                    //strA = str == "1" ? "0" : "1";
                    if (str == "Hiện")
                    {
                        strA = "0";
                    }
                    if (str == "Ẩn")
                    {
                        strA = "1";
                    }
                    Data.SqlDataProvider sql = new Data.SqlDataProvider();
                   
                    sql.ExecuteNonQuery("Update [Products] set PActive=" + strA + "  Where PId='" + strCA + "'");
                    BindGird();
                    break;
            }
        }
        //Reset
        private void Reset()
        {
            PImgEdit.ImageUrl = "";
            txtStatus.Text = "";
            lblFile.Text = "";
            txtAuthor.Text = "";
            txtPCode.Text = "";
            txtPName.Text = "";
            txtPPice.Text = "";
            txtSaleOff.Text = "";
            chkPActive.Checked = false;
            LoadDropDown();
            ddlCate.SelectedValue = "0";
            ddlPublish.SelectedValue = "0";
        }
        //Add Click
        protected void Add_Click(object sender, EventArgs e)
        {
            Insert = true;
            Reset();
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }
        //Delete Click
        protected void Delete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grdProducts.Items.Count; i++)
            {
                if (grdProducts.Items[i].ItemType == ListItemType.AlternatingItem || grdProducts.Items[i].ItemType == ListItemType.Item)
                {
                    if (((CheckBox)grdProducts.Items[i].FindControl("chkSelect")).Checked)
                    {
                        Business.ProductsService.Products_Delete(grdProducts.Items[i].Cells[1].Text);
                    }
                }
            }
            BindGird();
        }
        //Code Save File lên Server
        private string Save(FileUpload file)
        {
            string math = "";
            if (file.FileName != "")
            {
                math = Server.MapPath("/Upload/" + file.FileName);
                file.SaveAs(math);
                return "/Upload/" + file.FileName.ToString();
            }
            else
                return "";
        }
        //Save Click

        protected void Save_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
               
                Data.ProductsInfo obj=new Data.ProductsInfo();
               
                obj.PId = Id;
                obj.PCode =txtPCode.Text;
                obj.PName = txtPName.Text;
                obj.PPrice = txtPPice.Text;
                obj.PAuthor = txtAuthor.Text;
                obj.PSaleOff = txtSaleOff.Text;
                //obj.PImg = "";
                obj.PStatus = txtStatus.Text;
                obj.PUId = ddlPublish.SelectedValue;
                obj.CatId = ddlCate.SelectedValue;
                obj.AId = "1";
                obj.PActive = chkPActive.Checked ? "1" : "0";
                if (Insert == true)
                {
                    obj.PImg = this.Save(filePImg);
                    Business.ProductsService.Products_Insert(obj);
                }
                else
                {
                    if (this.Save(filePImg) != "")
                    {
                        obj.PImg = this.Save(filePImg);
                    }
                    else
                        obj.PImg = lblFile.Text;
                    Business.ProductsService.Products_Update(obj);
                }
                BindGird();
                pnUpdate.Visible = false;
                pnView.Visible = true;
            }
        }

        //Back Click
        protected void Back_Click(object sender, EventArgs e)
        {
           
            pnView.Visible = true;
            pnUpdate.Visible = false;
            BindGird();
        }
    }
}